package www.bplead.com.mydemo.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import www.bplead.com.mydemo.R;
import www.bplead.com.mydemo.adapter.MyPagerAdapter;
import www.bplead.com.mydemo.fragment.FoundFragment;
import www.bplead.com.mydemo.fragment.IndexFragment;
import www.bplead.com.mydemo.fragment.MyFragment;
import www.bplead.com.mydemo.util.SystemUtils;

public class LayoutViewPagerActivity extends AppCompatActivity implements View.OnClickListener{

    private static final String TAG = "LayoutViewPagerActivity";

    ViewPager myViewPager;
    TextView homeTV;
    TextView foundTV;
    TextView myTV;
    ImageView tabLineIV;

    View homeLayout;
    View foundLayout;
    View myLayout;

    private MyPagerAdapter myPagerAdapter;

    int currentIndex;
    int screenWidth;
    int tabWidth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewpager_main);

        setTitle("首页");

        myViewPager = (ViewPager) this.findViewById(R.id.id_page_vp);
        homeTV = (TextView) this.findViewById(R.id.id_home_tv);
        foundTV = (TextView) this.findViewById(R.id.id_found_tv);
        myTV = (TextView) this.findViewById(R.id.id_my_tv);
        tabLineIV = (ImageView) this.findViewById(R.id.id_tab_line_iv);

        homeLayout = this.findViewById(R.id.layout_home);
        foundLayout = this.findViewById(R.id.layout_found);
        myLayout = this.findViewById(R.id.layout_my);
        homeLayout.setOnClickListener(this);
        foundLayout.setOnClickListener(this);
        myLayout.setOnClickListener(this);

        screenWidth = SystemUtils.getScreenWidth(this);
        tabWidth = screenWidth / 3;

        ViewGroup.LayoutParams lp = tabLineIV.getLayoutParams();
        lp.width = tabWidth;
        tabLineIV.setLayoutParams(lp);

        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new IndexFragment());
        fragmentList.add(new FoundFragment());
        fragmentList.add(new MyFragment());
        myPagerAdapter = new MyPagerAdapter(getSupportFragmentManager(),fragmentList);

        myViewPager.setAdapter(myPagerAdapter);

        myViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                int marginRight = 0;
                //0 -> 1
                if(currentIndex == 0 && position == 0){
                    marginRight = (int) (tabWidth * positionOffset) + currentIndex * (screenWidth / 3);
                    moveTab(marginRight);
                }
                //1 -> 2
                else if(currentIndex == 1 && position == 1){
                    marginRight = (int) (tabWidth * positionOffset) + currentIndex * tabWidth;
                    moveTab(marginRight);
                }
                //2 -> 1
                else if(currentIndex == 2 && (position == 1)){
                    marginRight = (int) (tabWidth * (-(1-positionOffset))) + currentIndex * tabWidth;
                    moveTab(marginRight);
                }
                //1 -> 0
                else if(currentIndex == 1 && position == 0){
                    marginRight = (int) (tabWidth * (-(1-positionOffset))) + currentIndex * tabWidth;
                    moveTab(marginRight);
                }
                Log.v(TAG,"positionOffset>>" + positionOffset + "currentIndex>>" + currentIndex + "position>>" + position + "marginRight>>" + marginRight);
            }

            /**
             * @param position
             */
            @Override
            public void onPageSelected(int position) {
                //重置tab样式
                resetTabStyle();
                //设置当前tab样式
                switch (position){
                    case 0://首页
                        setTitle("首页");
                        homeTV.setTextColor(ContextCompat.getColor(LayoutViewPagerActivity.this,R.color.colorActive));
                        break;
                    case 1://发现
                        setTitle("发现");
                        foundTV.setTextColor(ContextCompat.getColor(LayoutViewPagerActivity.this,R.color.colorActive));
                        break;
                    case 2://我的
                        setTitle("我的");
                        myTV.setTextColor(ContextCompat.getColor(LayoutViewPagerActivity.this,R.color.colorActive));
                        break;
                }
                currentIndex = position;
            }

            /**
             * 屏幕状态发生改变时调用
             * @param state
             */
            @Override
            public void onPageScrollStateChanged(int state) {
                if(state == 0){
                    Log.v(TAG,"ViewPager>>>默认");
                }else if(state == 1){
                    Log.v(TAG,"ViewPager>>>正在滑动");
                }else if(state == 2){
                    Log.v(TAG,"ViewPager>>>滑动完毕");
                }
            }
        });
    }

    private void moveTab(int marginRight){
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) tabLineIV.getLayoutParams();
        lp.leftMargin = marginRight;
        tabLineIV.setLayoutParams(lp);
    }

    private void resetTabStyle(){
        homeTV.setTextColor(ContextCompat.getColor(LayoutViewPagerActivity.this,R.color.colorAccent));
        foundTV.setTextColor(ContextCompat.getColor(LayoutViewPagerActivity.this,R.color.colorAccent));
        myTV.setTextColor(ContextCompat.getColor(LayoutViewPagerActivity.this,R.color.colorAccent));
    }

    @Override
    public void onClick(View v) {
        int marginRight = 0;
        switch (v.getId()){//
            case R.id.layout_home:
                marginRight = tabWidth * 0;
                myViewPager.setCurrentItem(0,false);
                break;
            case R.id.layout_found:
                marginRight = tabWidth * 1;
                myViewPager.setCurrentItem(1,false);
                break;
            case R.id.layout_my:
                marginRight = tabWidth * 2;
                myViewPager.setCurrentItem(2,false);
                break;
        }
        moveTab(marginRight);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.v(TAG,"Activity State>>onDestroy()");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.v(TAG,"Activity State>>onStop()");
    }
}
